package com.haruhi.bot.job.thread;

import com.haruhi.bot.service.PixPictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 用于执行上传线程
 */
@Slf4j
@Component
public class PixUploadProxyThread {
    private static ThreadPoolExecutor pool =
            new ThreadPoolExecutor(20,40,5, TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(40) );


    public static class Task implements Runnable{
        private PixPictureService service;
        private String tag;
        private String name;
        private InputStream is;
        public Task(PixPictureService service,String name,InputStream is, String tag){
            this.service = service;
            this.tag = tag;
            this.name = name;
            this.is = is;
        }

        @Override
        public void run() {
            service.upload(name,is,tag);
        }
    }


    public static void execute(PixPictureService service, Map<String, InputStream> map,String tag){
        log.info("开始执行上传,文件数量:{}",map.size());

        for (Map.Entry<String, InputStream> entry : map.entrySet()) {
            pool.execute(new PixUploadProxyThread.Task(service,entry.getKey(),entry.getValue(),tag));
        }
    }
    @Async
    public void asyncExecute(PixPictureService service, Map<String, InputStream> map, String tag){
        PixUploadProxyThread.execute(service,map,tag);
    }
}
